//获取键盘事件
$(document).keydown(function (event) {
    //游戏结束状态下不能按按键继续游戏，必须重新开始游戏或者刷新页面才能继续游戏
    if (!isOver) {
        switch (event.keyCode) {
            case 74:
                if (board[3][0] === 1 && score === 0) {
                    timeRun();
                    clearText();
                    moveDown();
                } else if (board[3][0] === 1 && score > 0) {
                    moveDown();
                } else {
                    isGameOver();
                }
                break;
            case 75:
                // 如果敲击正确
                if (board[3][1] === 1 && score === 0) {
                    timeRun();
                    clearText();
                    moveDown();
                } else if (board[3][1] === 1 && score > 0) {
                    moveDown();
                } else {
                    isGameOver();
                }
                break;
            case 76:
                if (board[3][2] === 1 && score === 0) {
                    timeRun();
                    clearText();
                    moveDown();
                } else if (board[3][2] === 1 && score > 0) {
                    moveDown();
                } else {
                    isGameOver();
                }
                break;
        }
    }
})
//  清除提示内容的方法
function clearText () {
    $("#block-3-0").text("");
    $("#block-3-1").text("");
    $("#block-3-2").text("");
}
//黑块整体下移的方法
function moveDown () {
    for(var i = 3; i >=0; i--) {
        for(var j = 2; j >= 0; j--) {
            // 所有黑块向下移一格
            if (board[i][j] === 1) {
                if (i === 3) {
                    $("#block-" + i + "-" + j).css("background-color", "#fff");
                    board[i][j] = 0;
                } else {
                    $("#block-" + i + "-" + j).css("background-color", "#fff");
                    board[i][j] = 0;
                    $("#block-" + (i + 1) + "-" + j).css("background-color", "#000");
                    board[i + 1][j] = 1;
                }
            }
        }
    }
//    第一行再生成一个黑块
    var randy = parseInt(Math.floor(Math.random() * 3));
    var block = $("#block-0-" + randy);
    block.css("background-color", "#000");
    board[0][randy] = 1;
    score += 1
}
//计时器开始计时

function timeRun () {
    timerun += 0.001;
    $("span").text(timerun.toString().substr(0, 5));
    t = setTimeout("timeRun()", 1);
}

// 游戏结束函数
function isGameOver() {
    clearTimeout(t);
    $("#box_container").append("<div id='gameover' class='gameover'><p>本次用时:</p><span>" + timerun.toString().substr(0, 5) + "秒</span><a href='javascript:restartgame();' id='restartgamebutton'>再来一次</a></div>")
    $("#gameover").css("width", "300px");
    $("#gameover").css("height", "400px");
    $("#gameover").css("background-color", "rgba(0, 0, 0, 0.5)");
    isOver = true

}


//重新开始游戏
function restartgame () {
    $("#gameover").remove();
    $("#time_box").html("<span>0.000</span>" + "秒");
    $(".block").remove();
    score = 0;
    init();
    
    isOver = false
}